class WukongController {
  constructor(name, config, model, router) {
    if(!name && (typeof name) !== 'string')
      throw new Error('name requied and should be typeof String');
    this.name = name;
    this.config = config;
    this.Model = model;
    this.router = router;
    for (const key in this.Model) {
      const namespace = `/${key.toLocaleLowerCase()}s`;
      let getMiddleware = this.get;
      let getOneMiddleware = this.getOne;
      let updateMiddlerware = this.update;
      let postMiddlerware = this.post;
      let deleteMiddlerwaer = this.delete;
      if(this.config.hasOwnProperty(namespace)) {
        getMiddleware = this.config[namespace].get;
        getOneMiddleware = this.config[namespace].getOne;
        updateMiddlerware = this.config[namespace].update;
        postMiddlerware = this.config[namespace].post;
        deleteMiddlerwaer = this.config[namespace].deleteMiddlerwaer;
      }
      if(this.Model.hasOwnProperty(key)) {
        this.router.get(`${namespace}`, getMiddleware);
        this.router.get(`${namespace}/:id`, getOneMiddleware);
        this.router.post(`${namespace}`, postMiddlerware);
        this.router.put(`${namespace}/:id`, updateMiddlerware);
        this.router.delete(`${namespace}/:id`, deleteMiddlerwaer);
      }
    }
  }
  async dispatch(ctx) {
    const method = ctx.method;
    const query =  ctx.query;
    const body = ctx.request.body;
    await this[method.toLowercase()](ctx, query, body);
  }
  async get(ctx) {
    ctx.body = 'get';
  }
  async getOne(ctx) {
    ctx.body = 'get';
  }
  async post(ctx) {
    ctx.body = 'post';
  }
  async update(ctx) {
    ctx.body = 'put';
  }
  async delete(ctx) {
    ctx.body = 'delete';
  }

  toJson() {

  }

  router() {

  }
}

module.exports = WukongController;
